﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LicenseFileGenerator
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private string EncryptionKey = "cPPEI_cOolzOn";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (ctlDateExpire.SelectedDate == null)
            {
                MessageBox.Show("请输入过期日期");
                return;
            }

            if(ctlDateExpire.SelectedDate.Value <= DateTime.Now)
            {
                MessageBox.Show("过期日期无效");
                return;
            }

            string serialNumber = ctlEditSerialNumber.Text.Trim();
            string expireDate = ctlDateExpire.SelectedDate.Value.ToString("yyyy-MM-dd");
            string clientCount = ctlEditClientCount.Text.Trim();

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "*.lic|*.lic";
            if(dlg.ShowDialog() == true)
            {
                Dictionary<string, string> kv = new Dictionary<string, string>();
                kv.Add("EXPIREDATE", expireDate);
                kv.Add("CLIENTCOUNT", clientCount);
                // professional
                kv.Add("EDITION", "2");

                if (DSAL.LisenceFile.Save(dlg.FileName, EncryptionKey, serialNumber, kv))
                {
                    MessageBox.Show("创建成功");
                }
                else
                {
                    MessageBox.Show("创建失败");
                }
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
